package com.yk.spark.controller.app;

import com.yk.spark.service.manage.app.api.IAppService;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by Limit on 2017/8/23.
 */
@Controller
@RequestMapping("/app")
public class AppController {
    @Resource(name = "appService")
    private IAppService appService;


    @RequestMapping("createApp")
    @ResponseBody
    public Object createApp(
            @RequestParam(name = "appName") String appName
            ,@RequestParam(name = "appType") String appType
            ,@RequestParam(name = "parameters")String parameters) throws Exception {
        return this.appService.saveApp(appName,appType,parameters);
    }

    @RequestMapping("deleteApp")
    @ResponseBody
    public Object deleteApp( @RequestParam(name = "appId") String appId){
        return this.appService.deleteApp(appId);
    }

    @RequestMapping("updateApp")
    @ResponseBody
    public Object updateApp( @RequestParam(name = "appId") String appId
            ,@RequestParam(name = "input") String input
            ,@RequestParam(name = "output") String output){
        return this.appService.updateApp(appId,input,output);
    }

    @RequestMapping("listApp")
    @ResponseBody
    public Object listApp(@RequestParam(name = "start",defaultValue = "0") int start,@RequestParam(name = "limit",defaultValue = "10") int limit) throws JSONException {
        JSONObject result=new JSONObject();
        result.put("sum",this.appService.querySum());
        result.put("start",start);
        result.put("limit",limit);
        result.put("result",this.appService.getAppModel(start,limit));
        return result;
    }

    @RequestMapping("listAll")
    @ResponseBody
    public Object listAll() throws JSONException {
        JSONObject result=new JSONObject();
        result.put("result",this.appService.getAppModel());
        return this.appService.getAppModel();
    }

    @RequestMapping("getAppIO")
    @ResponseBody
    public Object getAppIO(@RequestParam(name = "appId") String appId) throws JSONException {
        return this.appService.getSelectFile(new JSONArray(this.appService.getAppModel(appId).getAppInput()));
    }

    @RequestMapping("getApp")
    @ResponseBody
    public Object getApp(@RequestParam(name = "appId") String appId) {
        return this.appService.getAppModel(appId);
    }

}
